package com.fivefu.core.skyeye.event.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fivefu.base.common.utils.str.StrUtils;
import com.fivefu.base.web.controller.BaseController;
import com.fivefu.base.web.vo.ResultInfo;
import com.fivefu.core.skyeye.event.services.DbAiSceneNodeService;
import com.fivefu.core.skyeye.event.utils.VerifyDataUtils;
import com.fivefu.core.skyeye.event.vo.SceneNodeBean;
import com.fivefu.core.skyeye.event.vo.SceneNodeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Map;

/**
 * <p>
 * 机器节点表 前端控制器
 * </p>
 *
 * @author 
 * @since 2022-08-25 09:23:44
 */
@Api(tags = "机器节点")
@RestController
@RequestMapping("/dbAiSceneNode")
public class DbAiSceneNodeController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(DbAiSceneNodeController.class);

    @Resource
    private DbAiSceneNodeService dbAiSceneNodeService;

    @RequestMapping(path = "/editSceneNode",method = RequestMethod.POST)
    @ApiOperation(value = "编辑机器节点",httpMethod = "POST",response = ResultInfo.class,notes = "编辑机器节点")
    public ResultInfo editSceneNode(@ModelAttribute @Valid SceneNodeVo sceneNodeVo, @RequestAttribute String userid){
        try {
            logger.info("编辑机器节点接收的参数："+JSON.toJSONString(request.getParameterMap()));
            String value = VerifyDataUtils.verifyRequiredColumn(sceneNodeVo);
            if (StrUtils.isNotNull(value)){
                return ResultInfo.renderError(value + "不能为空");
            }
            dbAiSceneNodeService.editSceneNode(sceneNodeVo,userid);
            return ResultInfo.renderSuccess("编辑机器节点成功");
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return   ResultInfo.renderError("编辑机器节点失败");
    }

    @RequestMapping(path = "/deleteSceneNode",method = RequestMethod.POST)
    @ApiOperation(value = "删除机器节点",httpMethod = "POST",response = ResultInfo.class,notes = "删除机器节点")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "机器节点编号", paramType = "query",required = true, dataType = "Long"),
    })
    public ResultInfo deleteSceneNode(Long id, @RequestAttribute String userid){
        try {
            logger.info("删除机器节点接收的参数："+ JSON.toJSONString(request.getParameterMap()));
            //判断机器节点编号是否为空
            if (id == null){
                return   ResultInfo.renderError("机器节点编号不能为空");
            }
            dbAiSceneNodeService.deleteSceneNode(id,userid);
            return ResultInfo.renderSuccess("删除机器节点成功");
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return   ResultInfo.renderError("删除机器节点失败");
    }

    @RequestMapping(path = "/querySceneNodeDetail",method = RequestMethod.POST)
    @ApiOperation(value = "获取机器节点详情",httpMethod = "POST",response = ResultInfo.class,notes = "获取机器节点详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "机器节点编号", paramType = "query",required = true, dataType = "Long")
    })
    public ResultInfo querySceneNodeDetail(Long id){
        try {
            logger.info("获取机器节点详情接收的参数："+ JSON.toJSONString(request.getParameterMap()));
            if (id == null){
                return   ResultInfo.renderError("机器节点编号不能为空");
            }
            Map<String, Object> sceneNode = dbAiSceneNodeService.querySceneNodeDetail(id);
            return ResultInfo.renderSuccess(sceneNode);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return   ResultInfo.renderError("获取机器节点详情失败");
    }

    @RequestMapping(path = "/querySceneNodeByPage",method = RequestMethod.POST)
    @ApiOperation(value = "(分页)获取机器节点列表",httpMethod = "POST",response = ResultInfo.class,notes = "(分页)获取机器节点列表")
    public ResultInfo querySceneNodeByPage(@ModelAttribute @Valid SceneNodeBean sceneNodeBean){
        try {
            logger.info("(分页)获取机器节点列表接收的参数："+ JSON.toJSONString(request.getParameterMap()));
            Page<Map<String, Object>> list = dbAiSceneNodeService.querySceneNodeByPage(sceneNodeBean);
            return ResultInfo.renderSuccess(list);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return   ResultInfo.renderError("(分页)获取机器节点列表失败");
    }

    @RequestMapping(path = "/querySceneNodeByCode",method = RequestMethod.POST)
    @ApiOperation(value = "根据探头标识获取机器节点",httpMethod = "POST",response = ResultInfo.class,notes = "根据场景探头标识获取机器节点")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cameraCode", value = "探头唯一标识", paramType = "query",required = true, dataType = "String")
    })
    public ResultInfo querySceneNodeByCode(String cameraCode){
        try {
            logger.info("根据探头标识获取机器节点："+ JSON.toJSONString(request.getParameterMap()));
            if (StrUtils.isNull(cameraCode)){
                return ResultInfo.renderError("探头唯一标识不能为空");
            }
            Map<String, Object> map = dbAiSceneNodeService.querySceneNodeByCode(cameraCode);
            return ResultInfo.renderSuccess(map);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return   ResultInfo.renderError("根据场景探头标识获取机器节点失败");
    }
}
